package com.zja.dao.Impl;

import com.zja.dao.ArticleDao;
import com.zja.dao.BaseDAO;
import com.zja.model.Article;

import java.util.List;

/**
 * @author JA
 * @date: 2025/4/28
 */
public class ArticleDaoImpl extends BaseDAO implements ArticleDao {
    @Override
    public List<Article> selectAll() {
        String sql = "SELECT * FROM article";
        try {
            return executeQuery(Article.class, sql);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Article selectById(Long id) {
        try {
            String sql = "SELECT * FROM article WHERE id = ?";
            return executeQueryBean(Article.class, sql, id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean insert(Article article) {
        try {
            String sql = "INSERT INTO article (title, content, author_id, knowledge_base_id, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)";
            return executeUpdate(sql, article.getTitle(), article.getContent(), article.getAuthorId(), article.getKnowledgeBaseId(), article.getCreatedAt(), article.getUpdatedAt());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean update(Article article) {
        try {
            String sql = "UPDATE article SET title = ?, content = ?, author_id = ?, knowledge_base_id = ?, updated_at = ? WHERE id = ?";
            return executeUpdate(sql, article.getTitle(), article.getContent(), article.getAuthorId(), article.getKnowledgeBaseId(), article.getUpdatedAt(), article.getId());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean deleteById(Long id) {
        try {
            String sql = "DELETE FROM article WHERE id = ?";
            return executeUpdate(sql, id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
